package com.atguigu.gmall.all.contoller;

import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.list.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author: 李旭
 * @create: 2021-07-19 09:25
 **/
@Controller
public class ListController {


    @Autowired
    private ListFeignClient listFeignClient;

    //进入搜索页面   **.suning.com
    //https://search.suning.com/手机/
    //https://search.jd.com/Search?keyword=手机&fffsf&fsf&fsf&
    //https://list.jd.com/list?keyword=手机
    @GetMapping("/list")
    public String listPage(SearchParam searchParam, Model model) {
        //远程调用
        SearchResponseVo vo = listFeignClient.list(searchParam);

        //1.回显入参条件数据 放在Request对象中
        model.addAttribute("searchParam", searchParam);
        //2.品牌集合
        List<SearchResponseTmVo> trademarkList = vo.getTrademarkList();
        model.addAttribute("trademarkList", trademarkList);
        //3.平台集合
        List<SearchResponseAttrVo> attrsList = vo.getAttrsList();
        model.addAttribute("attrsList", attrsList);

        //4:动态链接  URL
        String urlParam = buildUrlParam(searchParam);
        model.addAttribute("urlParam", urlParam);
        //5:商品集合
        List<Goods> goodsList = vo.getGoodsList();
        model.addAttribute("goodsList", goodsList);
        //6:分页 当前页 总页数
        model.addAttribute("totalPages", vo.getTotalPages());
        model.addAttribute("pageNo", vo.getPageNo());

        //7:排序Map
        Map<String, Object> orderMap = buildOrderMap(searchParam);
        model.addAttribute("orderMap", orderMap);
        //8:回显选中的品牌  //trademark=2:华为
        String trademarkParam = buildTrademarkParam(searchParam);
        model.addAttribute("trademarkParam", trademarkParam);
        //9:回显平台属性集合
        List<Map> propsParamList = buildPropsParamList(searchParam);
        model.addAttribute("propsParamList", propsParamList);
        return "list/index";
    }

    private String buildTrademarkParam(SearchParam searchParam) {
        String trademark = searchParam.getTrademark();
        if(!StringUtils.isEmpty(trademark)){
            return "品牌:" + trademark.split(":")[1];
        }
        return  null;
    }

    //平台属性集合
    private List<Map> buildPropsParamList(SearchParam searchParam) {
        ////   props=23:4G:运行内存
        String[] props = searchParam.getProps();
        if(null != props && props.length > 0){
            return Arrays.stream(props).map(prop -> {
                String[] p = prop.split(":");
                Map map  = new HashMap();
                map.put("attrName",p[2]);
                map.put("attrValue",p[1]);
                map.put("attrId",p[0]);
                return map;
            }).collect(Collectors.toList());
        }
        return null;
    }

    //排序Map
    private Map<String, Object> buildOrderMap(SearchParam searchParam) {
        Map<String, Object> orderMap = new HashMap<>();
        // order=1:asc
        String order = searchParam.getOrder();
        if (StringUtils.isEmpty(order)) {
            //默认
            orderMap.put("type", "1");
            orderMap.put("sort", "desc");
        } else {
            String[] o = order.split(":");
            orderMap.put("type", o[0]);
            orderMap.put("sort", o[1]);
        }
        return orderMap;
    }

    //动态链接  URL
    private String buildUrlParam(SearchParam searchParam) {

        StringBuilder sb = new StringBuilder();//线程不安全  性能高
        //StringBuffer sb = new StringBuffer();  线程安全  ArrayList  并发修改异常 性能低
        sb.append("http://list.gmall.com/list?");
        //关键词
        String keyword = searchParam.getKeyword();
        if (!StringUtils.isEmpty(keyword)) {
            sb.append("keyword=").append(keyword);
        }
        //品牌  品牌ID:品牌名称
        String trademark = searchParam.getTrademark();
        if (!StringUtils.isEmpty(trademark)) {
            sb.append("&trademark=").append(trademark);
        }
        //平台属性
        String[] props = searchParam.getProps();
        if (null != props && props.length > 0) {
            for (String prop : props) {
                sb.append("&props=").append(prop);
            }
        }
        return sb.toString();
    }

}
